<?php if(!defined('BASEPATH')) exit ('No direct script access allowed');

class Anniversary extends public_controller
{
	
	public function __construct() {
		parent:: __construct();
		$this->template->set_layout('person_layout');
		if(!$this->session->userdata('loginStatus')) redirect('landing');
		$this->light_candle = false;
	}

	public function index() {
		$current_person_id = (int)$this->uri->segment(2);
		$infoPerson = $this->person_model->get_person($current_person_id);
		$this->current_person_id = $current_person_id;

		if(!empty($infoPerson)) {
			//Check date of dead that same with currently day
			$dead_of_date = mdate("%m-%d", $infoPerson['day_death']);
			$now = date('m-d');
			if($dead_of_date === $now) {
				//reset light candle value
				$this->light_candle = TRUE;
			} else {
				$this->light_candle = FALSE;
			}

			$crumb = array(
				'Home'	=> base_url(),
				$infoPerson['name'] => base_url().'person/'. $infoPerson['id'] . '-' . $this->util->alias($infoPerson['name']),
				'Anniversary' => ''
			);

			$this->template->title($infoPerson['name'] . ' Anniversary', 'Dead project')
				->set(array('infoPerson' => $infoPerson, 'crumb' => $crumb))
				->build('anniversary/index');
		} else {
			redirect('home');
		}
	}

	public function post_candle() {
		$arr = array(
			'memId' => (int)$this->input->post('memId'),
			'personId' => (int)$this->input->post('personId'),
			'comment' => nl2br($this->input->post('comment')),
			'added_date' => time()
		);

		if($this->db->insert('person_candle', $arr)) {
			$info = array('status' => TRUE);
		} else {
			$info = array('status' => FALSE);
		}

		echo json_encode($info);
	}

	public function loadListCandle() {
		$person_id = (int)$this->input->post('personId');
		$this->db->order_by('id', 'desc');
		$this->db->limit(10);
		$sql = $this->db->get_where('person_candle', array('personId' => $person_id))->result_array();

		if(count($sql)) {
			foreach($sql as $row) {
				echo '<div class="col-lg-6" style="margin-bottom:20px; height:80px; overflow:hidden">';
				echo '<img src="'.base_url().'public/image/icon-candle.jpg" alt="" style="float:left; margin-right:10px">'; 
				echo '<p>'.$row['comment'].'</p>';
				echo '</div>';
			}

		} else {
			//echo "<p>No image from now</p>";
		}
	}

	public function load_more_candle(){
		$person_id = (int)$this->input->post('personId');
		$start = (int)$this->input->post('start');
		$this->db->order_by('id', 'desc');
		$this->db->limit(10, $start);
		$sql = $this->db->get_where('person_candle', array('personId' => $person_id))->result_array();

		if(count($sql)) {
			foreach($sql as $row) {
				
				echo '<div class="col-lg-6" style="margin-bottom:20px; height:80px; overflow:hidden">';
				echo '<img src="'.base_url().'public/image/icon-candle.jpg" alt="" style="float:left; margin-right:10px">'; 
				echo '<p>'.$row['comment'].'</p>';
				echo '</div>';
			}
		} else {
			//echo "<p>No image from now</p>";
		}	
	}
}
/*End file respected_people */
/* File local: application/controller/...*/